class_name Gamefield
extends Node2D

export (PackedScene) var generatecell
export (PackedScene) var generatefood
export (SpriteFrames) var redFrames
export (SpriteFrames) var blueFrames
export (Texture) var redTex
export (Texture) var blueTex

export var generatecount = 1
# Declare member variables here. Examples:
# var a = 2
# var b = "text"
export var food_chance = 0.3
# Called when the node enters the scene tree for the first time.
func _ready():
	initgame()
	pass # Replace with function body.

func createBlue(birthposition):
	var tempcell = generatecell.instance()
	tempcell.position = birthposition
	tempcell.team = 0
	tempcell.frametouse=blueFrames
	tempcell.textouse=blueTex
	tempcell.endinit()
	
func createRed(birthposition):
	var tempcell = generatecell.instance()
	tempcell.position = birthposition
	tempcell.team = 1
	tempcell.frametouse=redFrames
	tempcell.textouse=redTex
	tempcell.endinit()

func initgame():
	randomize()
	for i in range(generatecount):
		var tempcell = generatecell.instance()
		$ColorRect/Cells.add_child(tempcell)
		tempcell.position = Vector2(randi()%412+50,randi()%500 + 50)
		tempcell.team = 0
		tempcell.frametouse=blueFrames
		tempcell.textouse=blueTex
		tempcell.endinit()
		tempcell = generatecell.instance()
		$ColorRect/Cells.add_child(tempcell)
		tempcell.position = Vector2(randi()%412+552,randi()%500 + 50)
		tempcell.team = 1
		tempcell.frametouse=redFrames
		tempcell.textouse=redTex
		tempcell.endinit()
		
		
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	
	if Input.is_action_pressed("restart"):
		_restart()
#	pass


func _restart():
	for item in $ColorRect/Cells.get_children():
		#print(item.name, "removed!")
		if item is Cell:
			item.queue_free()
	for item in $ColorRect/Foods.get_children():
		#print(item.name, "removed!")
		item.queue_free()
	$TickTimer.one_shot = 0
	$TickTimer.start()
	initgame()

func createFood():
	var tempfood = generatefood.instance()
	$ColorRect/Foods.add_child(tempfood)
	tempfood.position = Vector2(randi()%914+50,randi()%500 + 50)

func _on_TickTimer_timeout():
	
	randomize()
	
	if randf() < food_chance:
		createFood()
	var team0count = 0
	var team1count = 0
	for cell in $ColorRect/Cells.get_children():
		if cell is Cell:
			if cell.team == 0:
				team0count += 1
			if cell.team == 1:
				team1count += 1
			cell.update()
	if team0count == 0:
		$Control/Label.text = "Team 1 wins!"
		$TickTimer.one_shot = 1;
		$WinningTimer.start()
	elif team1count == 0:
		$Control/Label.text = "Team 0 wins!"
		$TickTimer.one_shot = 1;
		$WinningTimer.start()
	else:
		$Control/Label.text = "Team0 " + str(team0count) + " : " + str(team1count) +" Team1"
	#$Label.text += "Ticktimer" + str($TickTimer.time_left)
	$TickTimer.wait_time = $Control/VSlider.value
	if $ColorRect/Cells.get_children().empty():
		$WinningTimer.stop()
		_restart()
		
	#create_texture_for_shader()
	
func split_cell(cellparent):
	var team0count = 0
	var team1count = 0
	var splitable = false
	for cell in $ColorRect/Cells.get_children():
		if cell is Cell:
			if cell.team == 0:
				team0count += 1
			if cell.team == 1:
				team1count += 1
	if (cellparent.team == 0 and team0count<150) or (cellparent.team == 1 and team1count<150):
		splitable = true
	if splitable:
		var tempcell = generatecell.instance()
		$ColorRect/Cells.add_child(tempcell)
		var genedirection = Vector2(randi() % 100 - 50, randi() % 100 - 50)
		genedirection = genedirection.normalized()
		#print(genedirection)
		tempcell.position = cellparent.position + 10 * genedirection
		tempcell.team = cellparent.team
		tempcell.frametouse = cellparent.frametouse
		tempcell.textouse = cellparent.textouse
		tempcell.lifespan = cellparent.lifespan/2
		cellparent.lifespan /= 2
		tempcell.speed = cellparent.speed + 5 * (randf() - 0.5)
		tempcell.attack = cellparent.attack + (randf() - 0.5)
		tempcell.search_range = cellparent.search_range + 10 * (randf() - 0.5)
		tempcell.attack_range = cellparent.attack_range + 10 * (randf() - 0.5)
		tempcell.endinit()
		#print("childbirth", tempcell.name)
	

#func create_texture_for_shader():
	#var image=Image.new()
	#image.create(300, 1, false, Image.FORMAT_RGBA8)
	#image.lock()
	#var cellcount = 0;
	#for cell in $Cells.get_children():
		#image.set_pixel(cellcount ,1 , Color(cell.position.x/1024, cell.position.y/600, cell.team, 1.0))
		#cellcount += 1
	#var imagetexture=ImageTexture.new()
	#imagetexture.create_from_image(image)
	#$Sprite.material.set_shader_param("centerss", imagetexture)
	#$Sprite.material.set_shader_param("cells", $Cells.get_child_count())
	#d$Sprite2.texture=imagetexture


func _on_WinningTimer_timeout():
	_restart()
	pass # Replace with function body.


func _on_Restart_pressed():
	_restart()
	pass # Replace with function body.
